package com.hdjd.controller;

import com.hdjd.entities.Account;
import com.hdjd.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘子强
 * 2021/1/23 17:32
 */
@Controller
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    //查询待审核的商家申请
    @ResponseBody
    @RequestMapping(value = "/businessApplyNeedCheck", method = RequestMethod.GET)
    public Map<String, Object> businessApplyNeedCheck(Integer schoolId, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<Account> accounts;
        Integer count;
        try {
             accounts = this.schoolService.businessApplyNeedCheck(schoolId, pageCurrent, pageSize);
             count = this.schoolService.countBusinessApplyNeedCheck(schoolId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "查询失败");
            return map;
        }
        if(!accounts.isEmpty()){
            map.put("code", 0);        //layui 查询成功必须返回 code = 0
            map.put("message", "查询成功");
            map.put("data", accounts);  //layui 查询必须返回的数据名 必须叫 data
            map.put("count", count);
            return map;
        }else{
            map.put("code", 0);
            map.put("message", "查询无果");
            return map;
        }
    }

    //查询已审核的商家申请
    @ResponseBody
    @RequestMapping(value = "/businessApplyChecked", method = RequestMethod.GET)
    public Map<String, Object> businessApplyChecked(Integer schoolId, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<Account> accounts;
        Integer count;
        try {
             accounts = this.schoolService.businessApplyChecked(schoolId, pageCurrent, pageSize);
             count = this.schoolService.countBusinessApplyChecked(schoolId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "查询失败");
            return map;
        }
        if(!accounts.isEmpty()){
            map.put("code", 0);        //layui 查询成功必须返回 code = 0
            map.put("message", "查询成功");
            map.put("data", accounts);  //layui 查询必须返回的数据名 必须叫 data
            map.put("count", count);
            return map;
        }else{
            map.put("code", 0);
            map.put("message", "查询无果");
            return map;
        }
    }

    //查询待审核的配送员申请
    @ResponseBody
    @RequestMapping(value = "/deliveryManApplyNeedCheck", method = RequestMethod.GET)
    public Map<String, Object> deliveryManApplyNeedCheck(Integer schoolId, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<Account> accounts;
        Integer count;
        try {
             accounts = this.schoolService.deliveryManApplyNeedCheck(schoolId, pageCurrent, pageSize);
             count = this.schoolService.countDeliveryManApplyNeedCheck(schoolId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "查询失败");
            return map;
        }
        if(!accounts.isEmpty()){
            map.put("code", 0);        //layui 查询成功必须返回 code = 0
            map.put("message", "查询成功");
            map.put("data", accounts);  //layui 查询必须返回的数据名 必须叫 data
            map.put("count", count);
            return map;
        }else{
            map.put("code", 0);
            map.put("message", "查询无果");
            return map;
        }
    }

    //查询已审核的配送员申请
    @ResponseBody
    @RequestMapping(value = "/deliveryManApplyChecked", method = RequestMethod.GET)
    public Map<String, Object> deliveryManApplyChecked(Integer schoolId, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String, Object> map = new HashMap<>();

        List<Account> accounts;
        Integer count;
        try {
            accounts = this.schoolService.deliveryManApplyChecked(schoolId, pageCurrent, pageSize);
            count = this.schoolService.countDeliveryManApplyChecked(schoolId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "查询失败");
            return map;
        }
        if(!accounts.isEmpty()){
            map.put("code", 0);        //layui 查询成功必须返回 code = 0
            map.put("message", "查询成功");
            map.put("data", accounts);  //layui 查询必须返回的数据名 必须叫 data
            map.put("count", count);
            return map;
        }else{
            map.put("code", 0);
            map.put("message", "查询无果");
            return map;
        }
    }

    //审核通过商家申请
    @ResponseBody
    @RequestMapping(value = "/passBusinessApply", method = RequestMethod.PUT)
    public Map<String, Object> passBusinessApply(Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.passBusinessApply(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "审核成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
    }

    //审核不通过商家申请
    @ResponseBody
    @RequestMapping(value = "/unPassBusinessApply", method = RequestMethod.PUT)
    public Map<String, Object> unPassBusinessApply(Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.unPassBusinessApply(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "审核成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
    }

    //审核通过配送员申请
    @ResponseBody
    @RequestMapping(value = "/passDeliverymanApply", method = RequestMethod.PUT)
    public Map<String, Object> passDeliverymanApply(Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.passDeliverymanApply(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "审核成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
    }

    //审核不通过配送员申请
    @ResponseBody
    @RequestMapping(value = "/unPassDeliverymanApply", method = RequestMethod.PUT)
    public Map<String, Object> unPassDeliverymanApply(Integer accId){
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.unPassDeliverymanApply(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("message", "审核成功");
            return map;
        }else{
            map.put("code", 1002);
            map.put("message", "审核失败");
            return map;
        }
    }

}
